package com.luqh.note.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.luqh.note.dao.NoteDao;
import com.luqh.note.pojo.Note;
import com.luqh.note.pojo.NoteExample;
import com.luqh.note.service.NoteService;

@Service
public class NoteServiceImpl implements NoteService {

	@Autowired
	private NoteDao noteDao;
	
	/**
	 * 所有的笔记列表
	 */
	@Override
	public List<Note> selectAllNote() {
		NoteExample example = new NoteExample();
		example.setOrderByClause("create_time desc");
		
		return noteDao.selectByExampleWithBLOBs(example);
	}
	/**
	 * 添加笔记
	 * @param note
	 * @return
	 */
	@Override
	public boolean addNote(Note note) {
		String id = System.currentTimeMillis() + RandomUtils.nextInt(0, 100) + "";//主键
		note.setId(id);
		note.setCreateTime(new Date());
		note.setUpdateTime(new Date());
		return noteDao.insertSelective(note) > 0 ? true:false;
	}
	/**
	 * 搜索
	 */
	@Override
	public List<Note> selectBySearch(String key, String level, String categoryId) {
		List<Note> list = null;
		NoteExample example = new NoteExample();
		NoteExample.Criteria criteria = example.createCriteria();
		example.setOrderByClause("create_time desc");//按照创建时间排序
		if(StringUtils.isNotEmpty(key)) {
			criteria.andTitleLike("%"+key+"%");
		}
		if(StringUtils.isNotEmpty(categoryId)) {
			if(StringUtils.equals("2", level)) {  //二级分类
				criteria.andCategoryIdEqualTo(categoryId);
				list = noteDao.selectByExampleWithBLOBs(example);
			}else if(StringUtils.equals("1", level)) { //一级分类
				
			}
		}else {
			list = noteDao.selectByExampleWithBLOBs(example);
		}
		
		return list;
	}
	public static void main(String[] args) {
		System.out.println(RandomUtils.nextInt(0, 10));
	}
	
}
